Skip to content

PSG: a coast the optimizer shortened to nothing is no coast - #2735

Closed
sarbian wants to merge 1 commit into
devfrom
fix/psg-zero-coast-during
Closed

sarbian wants to merge 1 commit into
devfrom
fix/psg-zero-coast-during

Conversation

@sarbian

@sarbian sarbian commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

PSG can plan a coast of no length. When that coast is a coast-during, guidance then burns the stage until it's empty instead of stopping at the target orbit.

What happened

Seen in MechJeb2_UI Spike 106 (run rework_during): Kerbal XI to 145 km with a coast during stage 2, flown on the PC. The ascent lofted early, and from the pad onward every solve gave the coast a length of 0 (bt: 0 <= 0.0000 <= 1.0). Burning straight through was the optimum, so that much is legitimate with a minimum coast of 0.

Guidance then never left BURNING. Solution.WillCoast counts any coast phase ahead, including one of no length. The coast-during hold in HandleTerminal keeps terminal guidance off while WillCoast is true, and every re-solve put the zero-length coast ahead again. Stage 2 burned to empty and guidance went BURNING → FINISHED at −1167 × 10,947 km.

The fix

WillCoast and CoastFollows skip a coast shorter than a millionth of the trajectory (Solution.FlownCoast). The burns either side of it are flown as one:

  • no coast-during hold;
  • autostaging limited to the terminal stage, not the coast stage;
  • terminal guidance at the end of the second burn: TgoForKSPStage already sums both burns;
  • TerminalDone doesn't start a coast after the shutdown.

Coast(t) never landed in a zero-length phase (IndexFor skips it), so it's unchanged.

Tests

  • SolutionTests.ZeroLengthCoastIsNoCoast: a burn, a coast of no length and a burn in one KSP stage. Coast, WillCoast and CoastFollows are false throughout, and the stage's burn time left counts both burns. It fails without the fix ("Expected false, but was true").
  • MechJebLibTest: 15,373 passed, 0 failed.

Not flown: a zero-length coast-during can't be set up on demand in game.

With a minimum coast of zero, PSG can plan a coast of no length when
burning straight through is better. Solution.WillCoast still counted it,
so guidance's coast-during hold kept terminal guidance off for a coast
that never came: every re-solve put the zero-length coast ahead again,
and the stage burned until it was empty, far past the target orbit.

WillCoast and CoastFollows now skip a coast shorter than a millionth of
the trajectory. The burns either side of it are flown as one, with
terminal guidance at the end of the second.
@lamont-granquist

Copy link
Copy Markdown
Collaborator

This should really get pruned in the SolutionBuilder. I thought I already did this, but there's already code there to prune out incredibly short burn segments.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Build Artifact 🚀

MechJeb2-12262fd.zip (built from 0b8e2bc)

@lamont-granquist

Copy link
Copy Markdown
Collaborator

nope AnalyzeStages() doesn't prune them out, it probably should, but the old Solution is required to warm start the optimizer on the next loop and keeping the phases in sync makes all that a bit more sane and realiable.

@lamont-granquist

Copy link
Copy Markdown
Collaborator

Okay I still think what likely needs to happen is that AnalyzeStages() should mark the phase as "Skip" or "Prune" for coasts and burns that don't have enough time to be meaningful because the optimizer shrunk them. Then the Solution only needs to be responsible for skipping those phases, and it needs to 'know' a lot less about trajectory rules.

@lamont-granquist

Copy link
Copy Markdown
Collaborator

This should hopefully be fixed by #2777

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants